package cn.wenhaha.common.utils;

import cn.wenhaha.common.exception.ApiMSGException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 参数验证
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-07-10 22:18
 */
@Component
public class ValidatorUtil {

    @Autowired
    private  Validator validator;


    public void validate(Object bean){
        Set<ConstraintViolation<Object>> validate = validator.validate(bean);
        int size = validate.size();
        if (size!=0){
            for (ConstraintViolation<Object> next : validate) {
                String message = next.getMessage();
                throw  ApiMSGException.build(-1, message);
            }
        }
    }



}
